package com.qf.listener;

import com.qf.constant.RabbitMQConstants;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainManager;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * projectName: sms-platform02
 *
 * @author: 元
 * time: 2021/10/16 0016 17:37
 * description:
 */
@Component
@Slf4j
public class PreSendQueueListener {

    @Autowired
    private FilterChainManager filterChainManager;

    @RabbitListener(queues = RabbitMQConstants.TOPIC_PRE_SEND)
    public void messageHandler(StandardSubmit standardSubmit, Channel channel, Message message){
        log.info("[策略模块~接受短信数据],接受短信的对象为:StandardSubmit={}",standardSubmit);

        //1.调用servie层,对短信对象进行策略处理
        filterChainManager.execute(standardSubmit);
        //2.对消息进行手动ACK
        try{
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (IOException e){
            e.printStackTrace();
            log.info("[策略模块~接受短信数据,执行失败],短信对象为:StandardSubmit={}",standardSubmit);
        }


    }
}
